<template>
	<view class="index-content cmmon-page-bg">
		<!-- 顶部轮播图 -->
		<view class="swiper-box">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"
				ndicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff">
				<swiper-item>
					<image
						src="https://img0.baidu.com/it/u=968073716,3865842427&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500" 
						mode="aspectFill" alt="" 
					/>
				</swiper-item>
				<swiper-item>
					<image 
						src="https://img2.baidu.com/it/u=2065454279,2043168215&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067" 
						mode="aspectFill" 
						alt="" />
				</swiper-item>
				<swiper-item>
					<image 
						src="https://img.zcool.cn/community/0106d86346e8de000e881000a441ce.jpg?x-oss-process=image/auto-orient,1/resize,m_lfit,w_1280,limit_1/sharpen,100" 
						mode="aspectFill" alt="" />	
				</swiper-item>
			</swiper>
		</view>
		<!-- 公告栏 -->
		<view class="hot-box">
			<view class="left">
				<uni-icons type="sound-filled" size="26"></uni-icons>
				<text>公告</text>
			</view>
			<view class="center">
				<swiper vertical autoplay interval="1500" duration="1000" circular>
					<swiper-item v-for="item in 4">
						滚动文字滚动文字滚动文字滚动文字滚动文字
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="18" color="#333"></uni-icons>
			</view>
		</view>
		<!-- 每日推荐，新闻列表等 -->
		<view class="news-box">
			<common-title>
				<template #title>
					每日推荐
				</template>
				<template #custom>
					<view class="right-custom">
						<uni-icons type="calendar" size="18" color="28b389"></uni-icons>
						<view class="text">
							<uni-dateformat :date="Date.now()" format="dd"></uni-dateformat>
							日
						</view>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x>
					<view class="detail-box" v-for="item,index in newsData" :key="item._id">
						<image lazy-load :src="item.url" mode="scaleToFill" alt="" @click="onPreview(index)"/>
					</view>
					<!-- <view class="card-header">&emsp;卡片标题卡片标题卡片标题 ----- author</view> -->
				</scroll-view>
			</view>
		</view>
		<!-- 专题精选 -->
		<view class="theme-box">
			<common-title>
				<template #title>
					专题精选
				</template>
				<template #custom>
					<navigator url="" class="more">More+</navigator>
				</template>
			</common-title>
			<view class="content">
				<theme-item v-for="item,index in 8" :key="index"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
		<!-- 置顶、刷新按钮区域 -->
		<view class="index-float">
			<view class="item" @click="onRefresh()">
				<uni-icons type="refreshempty" size="27" color="#888"></uni-icons>
			</view>
			<view class="item" @click="onGoTop()">
				<uni-icons type="arrow-up" size="27" color="#888"></uni-icons>
			</view>
		</view>
		<!-- 底部加载更多区域 -->
		<!-- <view class="index-load-more">
			<uni-load-more status="loading"></uni-load-more>
		</view> -->
	</view>
</template>

<script>
import { onReachBottom,onPullDownRefresh } from "@dcloudio/uni-app"
export default {
	data() {
		return {
			newsData: [],
			loadPicCount: 5
		}
	},
	onLoad() {
		console.log('index onload')
		// this.queryBookList()
	},
	created() {
		console.log('index created')
		this.onLoadIndexNews()
	},
	methods: {
		onLoadIndexNews(){
			uni.showLoading({
				title: '数据加载中',
				mask: true
			})
			
			uni.request({
				// https://api.vvhan.com/api/wallpaper/acg?type=json
				// https://api.uomg.com/api/rand.img3?sort=胖次猫&format=json
				// https://tea.qingnian8.com/tools/taoShow  data: {size:5}
				// https://tea.qingnian8.com/tools/petShow  data: {size:5,type: 'dog'}  type:all/dog/cat
				url: "https://tea.qingnian8.com/tools/taoShow",
				data:{
					size: 5
				},
				method:'GET'
			}).then(res => {
				// console.log("结果", res)
				if(res.statusCode === 200 && res.data.errCode === 200){
					// 触底后新的还在上面
					this.newsData = [...this.newsData, ...res.data.data]
				}
			}).finally(()=>{
				uni.hideLoading()
			})
		},
		// 点击预览
		onPreview(index){
			let urls = this.newsData.map(item => item.url)
			uni.previewImage({
				urls: urls,
				current:index
			})
		},
		// 点击刷新
		onRefresh(){
			uni.startPullDownRefresh()
		},
		// 点击回到顶部
		onGoTop(){
			uni.pageScrollTo({
				scrollTop:0,
				duration:100
			})
		}
	},
	// 触底事件
	onReachBottom(){
		console.log("index触底")
		this.onLoadIndexNews()
	},
	// 下拉刷新
	onPullDownRefresh(){
		console.log("index刷新")
		this.newsData = []
		this.onLoadIndexNews()
		uni.stopPullDownRefresh()
	}
}
</script>

<style lang="scss" scoped>
.index-content{
	width: 750rpx;
	// height: 100%;
	.swiper-box {
		width: 750rpx;
		padding: 30rpx 0;
		swiper{
			width: 750rpx;
			height: 340rpx;
			&-item{
				width: 100%;
				height: 100%;
				// common-style.scss中定义内置padding
				padding: 0 30rpx;
				image{
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
	}
	.hot-box{
		// 左右各缩进30：750-60 = 690
		width: 690rpx;
		height: 80rpx;
		line-height: 80rpx;
		margin: 0 auto;
		background-color: #f9f9f9;
		border-radius: 60rpx;
		display: flex;
		.left{
			width: 70px;
			display: flex;
			align-items: center;
			justify-content: center;
			::v-deep.uni-icons{
				color: $common-icon-theme-color !important;
			}
			text{
				color: $common-icon-theme-color;
				font-weight: 600;
				font-size: 30rpx;
			}
		}
		.center{
			flex: 1; //中间沾满
			swiper{
				height: 100%;
				&-item{
					height: 100%;
					font-size: 30rpx;
					color: #666;
					overflow: hidden;
					// 文字超出长度不换行
					white-space: nowrap;
					// 超出部分 ...显示
					text-overflow: ellipsis;
				}
			}
		}
		.right{
			width: 70rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
	.news-box{
		padding-top: 50rpx;
		.right-custom{
			color: $common-icon-theme-color;
			display: flex;
			align-items: center;
			.text{
				margin-left: 8rpx;
			}
		}
		.content{
			// 750 - 30(左测边距) (右侧不缩进，保留一种可滑动的效果)
			width: 720rpx;
			margin-left: 30rpx;
			margin-top: 30rpx;
			scroll-view{
				white-space: nowrap;
				.detail-box{
					width: 200rpx;
					height: 430rpx;
					display: inline-block;
					margin-right: 15rpx;
					image{
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
				.detail-box:last-child{
					margin-right: 30rpx;
				}
			}
		}
	}
	.theme-box{
		padding: 50rpx 0;
		.more{
			font-size: 32rpx;
			color: #888;
		}
		.content{
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			gap: 15rpx;
			// 网格布局，每行3个，每个宽高一致
			grid-template-columns: repeat(3, 1fr);
		}
	}
	.index-float{
		position: fixed;
		right: 30rpx;
		bottom: 80rpx;
		// env(safe-area-inset-bottom)  安全高度，即手机底部的home部分
		padding-bottom: env(safe-area-inset-bottom);
		.item{
			width: 90rpx;
			height: 90rpx;
			background: rgba(255, 255, 255, 0.9);
			border-radius: 50%;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: 1px solid #eee;
		}
	}
	.index-load-more{
		padding-bottom: calc(env(safe-area-inset-bottom) + 50rpx);
	}
}
	
</style>
